<?php
//上传漫画内容图片
$imgUrl = "";
// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["img_Url"]["name"]);
$extension = end($temp);        // 获取文件后缀名
//下面的判断为：文件类型必须为图片类，大小必须为200kb以内，后缀必须为：gif，jpeg、jpg、png
if ((($_FILES["img_Url"]["type"] == "image/gif")
        || ($_FILES["img_Url"]["type"] == "image/jpeg")
        || ($_FILES["img_Url"]["type"] == "image/jpg")
        || ($_FILES["img_Url"]["type"] == "image/pjpeg")
        || ($_FILES["img_Url"]["type"] == "image/x-png")
        || ($_FILES["img_Url"]["type"] == "image/png"))
    && ($_FILES["img_Url"]["size"] < 8192000)    // 小于 8000 kb
    && in_array($extension, $allowedExts))
{
    move_uploaded_file($_FILES["img_Url"]["tmp_name"],'../uploads/contentImg/'.$_FILES["img_Url"]["name"]);
    $imgUrl = "/back/uploads/contentImg/".$_FILES["img_Url"]["name"];
}
else{
    die("未选择文件或非法文件！");
}
//上传漫画章节封面图
$sectionImg = "";
// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["section_Img"]["name"]);
$extension = end($temp);        // 获取文件后缀名
//下面的判断为：文件类型必须为图片类，大小必须为200kb以内，后缀必须为：gif，jpeg、jpg、png
if ((($_FILES["section_Img"]["type"] == "img/gif")
        || ($_FILES["section_Img"]["type"] == "image/jpeg")
        || ($_FILES["section_Img"]["type"] == "image/jpg")
        || ($_FILES["section_Img"]["type"] == "image/pjpeg")
        || ($_FILES["section_Img"]["type"] == "image/x-png")
        || ($_FILES["section_Img"]["type"] == "image/png"))
    && ($_FILES["section_Img"]["size"] < 8192000)    // 小于 8000 kb
    && in_array($extension, $allowedExts))
{
    move_uploaded_file($_FILES["section_Img"]["tmp_name"],'../uploads/sectionImg/'.$_FILES["section_Img"]["name"]);
    $sectionImg = "/back/uploads/sectionImg/".$_FILES["section_Img"]["name"];
//    echo  "文件以保存到：uploads/".$_FILES["img"]["name"];
}
else{
    die("未选择文件或非法文件！");

}
// 判断参数是否完整
if(empty($_POST["pro_id"])) {
    die("作品名不能为空！");
}
if(empty($_POST["csection"])){
    die("章节序号不能为空！");
}
if($_POST["csection"]<0){
    die("章节序号非法！");
}
if(empty($_POST["section_Name"])){
    die("章节标题不能为空！");
}

//  获取参数，根据参数生成insert 语句
$proId = $_POST["pro_id"]; // 接收漫画作品ID
$section = $_POST["csection"];   // 接收漫画章节序号
$sectionName = $_POST["section_Name"];   // 接收漫画章节名

$sql = "insert into content(proId,section,sectionImg,sectionName,imgUrl) values ";
$sql .= "('$proId','$section','$sectionImg','$sectionName','$imgUrl')";
//进行数据库操作，将insert语句发送至数据库
//建立数据库连接
include_once dirname(__FILE__) .'../../common/conn.php';
//发送 insert 语句，如果执行成功返回true，否则返回false；
$res = mysqli_query($con,$sql);
//关闭数据库
mysqli_close($con);
// 判断语句执行结果，如果成功，提示用户跳转至列表页查看
if($res) // 插入成功！
{
    echo "<script> {window.alert('漫画内容添加成功');location.href='clist.php'} </script>";
    exit;
}else{// 5. 如果失败，提示用户重新录入
    echo "<script> {window.alert('漫画内容添加失败');location.href='contentAdd.php'} </script>";
    exit;
}
